﻿using System;

namespace EmbeddedSharp.Core.Function.Maths {

    /// <summary>
    /// Generate a random number within range.
    /// 
    /// Syntax: Random();
    /// 
    /// Example:
    /// rnd = Random();
    /// Echo(rnd);
    /// </summary>
    public class RandomNode : OperatorNode {
        public RandomNode()
            : base() {
            Value = "random";
        }

        public override object Evaluate() {
            Random ran = new Random();

            switch (this.Parameters.Count) {
                case 0:
                    return ran.Next();
                case 1:
                    int maxValue
                        = Convert.ToInt32(this.Parameters[0].Evaluate());
                    return ran.Next(maxValue);
                case 2:
                    maxValue
                        = Convert.ToInt32(this.Parameters[1].Evaluate());
                    int minValue
                        = Convert.ToInt32(this.Parameters[0].Evaluate());
                    return ran.Next(minValue, maxValue);
                default:
                    throw new Exception();
            }
        }
    }
}
